/**
 * Created by yanghaibo on 2016/10/25.
 */
ActivityManageController = function() {
  // 获取订单详情
  this.getActivityList = function(param, resetFunc) {
    var serverUrl = App.serverUrl + "merchantApp/activity/list.do";

    var sucFunc = function(result) {
      if (result && result.success) {
        // 服务器返回成功

        activityManageParam.inited = true;
        var pageData = result.resultObject;

        // 格式化后端数据
        var controller = new ActivityManageController();
        for (var i = 0; i < pageData.pageData.length; i++) {
          var activity = pageData.pageData[i];
          controller.formatActivityVo(activity);
        }
        if (param.pageNo === 1 && pageData.pageData.length === 0) {
          CommonUtil.render(
            $("#activityManagePage .activity-list"),
            "activityListNoDataTpl",
            {}
          );
        } else {
          CommonUtil.render(
            $("#activityManagePage .activity-list"),
            "activityListTpl",
            { data: pageData.pageData},
            param.pageNo > 1
          );
        }

        if (param.pageNo === 1) {
          initAddMoreItem(
            "#activityManagePage .page-body .activity-list-wrap-content"
          );
        }
        if (pageData.nextPage > 0) {
          // TODO 显示更多
          resetFunc && resetFunc();
        } else {
          // TODO 没有更多了
          resetFunc && resetFunc(true);
        }
      } else {
        DialogUtil.dialogWarn(result.msg);
      }
    };

    CommonUtil.sendJsonpRequest({
      url: serverUrl,
      params: param,
      success: sucFunc
    });
  };

  this.formatActivityVo = function(activityVo) {
    activityVo.ticketCount = activityVo.promTools.length; // 券数量
    var ticketNames = [];
    for (var i = 0; i < activityVo.promTools.length; i++) {
      var promTool = activityVo.promTools[i];
      var promToolName;
      if (
        activityVo.activityMarketType == 22 ||
        activityVo.activityMarketType == 51
      ) {
        promToolName = ActivityUtil.parseVoucherName(promTool);
      } else {
        promToolName = promTool.voucherName;
      }
      ticketNames.push(promToolName);
    }
    activityVo.writeOffRate = activityVo.writeOffRate || "0%";
    activityVo.ticketName = ticketNames.join("、"); // 活动列表显示的券名称
    activityVo.statusChn = ActivityUtil.parseActivityStatus2Ch(
      activityVo.activityStatus
    ); // 状态中文
    if (activityVo.activityMarketType == 43) {
      activityVo.activityTypeChn = "挽回流失会员";
    } else {
      var activityTypeConst = ActivityUtil.getActivityByType(
        activityVo.activityMarketType
      );
      activityVo.activityTypeChn = activityTypeConst
        ? activityTypeConst.des
        : "其他";
    }
  };
};
